package kr.hkit.summary_java.serviceui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.db.DbUtil;
import kr.hkit.summary_java.dao.CodeTitleDAO;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.CodeTitle;

public class TotalFrame extends JFrame implements ActionListener {
	private JMenuBar menuBar;
	private JPanel contentPane;
	private String[] menuTitle; // 메뉴타이틀 값을 담기 위한 배열
	private DepartmentDAO ddao;
	private EmployeeDAO edao;
	private CodeTitleDAO cdao;


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Connection con = ConnectionFactory.getConnection();
					TotalFrame frame = new TotalFrame(con);

					frame.addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
							DbUtil.close(con);
						}

					});
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TotalFrame(Connection con) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		menuBar = new JMenuBar();

		MainMenu jMenuDepartment = new MainMenu("부서");

		menuBar.add(jMenuDepartment);

		MainMenu jMenuEmployee = new MainMenu("사원");
		menuBar.add(jMenuEmployee);

		MainMenu jMenuTitle = new MainMenu("직책");
		menuBar.add(jMenuTitle);

		setJMenuBar(menuBar);

		addAction();
		
		ddao = new DepartmentDAO(con);
		edao = new EmployeeDAO(con);
		cdao = new CodeTitleDAO(con);
	}

	private void addAction() {
		int menuCnt = menuBar.getMenuCount();
		menuTitle = new String[menuCnt];

		for (int i = 0; i < menuCnt; i++) {
			menuTitle[i] = menuBar.getMenu(i).getText();
		}

		for (int j = 0; j < menuCnt; j++) {
			for (int i = 0; i < menuBar.getMenu(j).getItemCount(); i++) {
				menuBar.getMenu(j).getItem(i).addActionListener(this);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == menuBar.getMenu(0).getItem(0)) {
			actionPerformedMntmAdd(e);
		}
		if (e.getSource() == menuBar.getMenu(0).getItem(1)) {
			actionPerformedMntmSearch(e);
		}
		if (e.getSource() == menuBar.getMenu(0).getItem(2)) {
			actionPerformedMntmUpdate(e);
		}
		if (e.getSource() == menuBar.getMenu(0).getItem(3)) {
			actionPerformedMntmDel(e);
		}
		if (e.getSource() == menuBar.getMenu(0).getItem(4)) {
			actionPerformedMntmList(e);
		}
		if(e.getSource() == menuBar.getMenu(1).getItem(0)){
			actionPerformedEmpMntmAdd(e);
		}
		if(e.getSource() == menuBar.getMenu(1).getItem(1)){
			actionPerformedEmpMntmSearch(e);
		}
		if(e.getSource() == menuBar.getMenu(1).getItem(2)){
			actionPerformedEmpMntmUpdate(e);
		}
		if(e.getSource() == menuBar.getMenu(1).getItem(3)){
			actionPerformedEmpMntmDel(e);
		}
		if(e.getSource() == menuBar.getMenu(1).getItem(4)){
			actionPerformedEmpMntmList(e);
		}
		if(e.getSource() == menuBar.getMenu(2).getItem(0)){
			actionPerformedCodeMntmAdd(e);
		}
		if(e.getSource() == menuBar.getMenu(2).getItem(1)){
			actionPerformedCodeMntmSearch(e);
		}
		if(e.getSource() == menuBar.getMenu(2).getItem(2)){
			actionPerformedCodeMntmUpdate(e);
		}
		if(e.getSource() == menuBar.getMenu(2).getItem(3)){
			actionPerformedCodeMntmDel(e);
		}
		if(e.getSource() == menuBar.getMenu(2).getItem(4)){
			actionPerformedCodeMntmList(e);
		}
	}
	private void revalidPanel(Panel_EmployeeUITest panel) {
		contentPane.removeAll();
		contentPane.add(panel);
		revalidate();
	}
	
	private void revalidPanel(Panel_DepartmentUITest panel) {
		contentPane.removeAll();
		contentPane.add(panel);
		revalidate();
	}
	
	private void revalidPanel(Panel_CodeTitleUITest panel) {
		contentPane.removeAll();
		contentPane.add(panel);
		revalidate();
	}

	protected void actionPerformedMntmUpdate(ActionEvent e) {
		revalidPanel(new Panel_DepartmentUITest("수정", ddao));
	}

	protected void actionPerformedMntmAdd(ActionEvent e) {
		revalidPanel(new Panel_DepartmentUITest("추가", ddao));
	}

	private void actionPerformedMntmSearch(ActionEvent e) {
		revalidPanel(new Panel_DepartmentUITest("검색", ddao));
	}

	protected void actionPerformedMntmDel(ActionEvent e) {
		revalidPanel(new Panel_DepartmentUITest("삭제", ddao));
	}

	protected void actionPerformedMntmList(ActionEvent e) {
		contentPane.removeAll();
		PanelListUI panelList = new PanelListUI();
		panelList.setDao(ddao);
		contentPane.add(panelList);
		revalidate();
	}
	
	private void actionPerformedEmpMntmDel(ActionEvent e) {
		revalidPanel(new Panel_EmployeeUITest("삭제", edao));
		
	}

	private void actionPerformedEmpMntmUpdate(ActionEvent e) {
		revalidPanel(new Panel_EmployeeUITest("수정", edao));
		
	}

	private void actionPerformedEmpMntmSearch(ActionEvent e) {
		revalidPanel(new Panel_EmployeeUITest("검색", edao));
		
	}

	private void actionPerformedEmpMntmAdd(ActionEvent e) {
		revalidPanel(new Panel_EmployeeUITest("추가", edao));
		
	}
	
	private void actionPerformedEmpMntmList(ActionEvent e) {
		contentPane.removeAll();
		PanelListUIEmp panelList = new PanelListUIEmp();
		panelList.setDao(edao);
		contentPane.add(panelList);
		revalidate();
		
	}
	
	private void actionPerformedCodeMntmDel(ActionEvent e) {
		revalidPanel(new Panel_CodeTitleUITest("삭제", cdao));
		
	}

	private void actionPerformedCodeMntmUpdate(ActionEvent e) {
		revalidPanel(new Panel_CodeTitleUITest("수정", cdao));
		
	}

	private void actionPerformedCodeMntmSearch(ActionEvent e) {
		revalidPanel(new Panel_CodeTitleUITest("검색", cdao));
		
	}

	private void actionPerformedCodeMntmAdd(ActionEvent e) {
		revalidPanel(new Panel_CodeTitleUITest("추가", cdao));
		
	}
	
	private void actionPerformedCodeMntmList(ActionEvent e) {
		contentPane.removeAll();
		PanelListUICode panelList = new PanelListUICode();
		panelList.setDao(cdao);
		contentPane.add(panelList);
		revalidate();
		
	}
	
}
